package com.aiwhose.aiwhosewechatgroup.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aiwhose.aiwhosecore.bean.Result;
import com.aiwhose.aiwhosecore.bean.wechatgroup.WxGroupQr;
import com.aiwhose.aiwhosecore.bean.wechatgroup.WxGroupTypes;
import com.aiwhose.aiwhosecore.bean.wechatgroup.vo.WxGroupTypesVO;
import com.aiwhose.aiwhosewechatgroup.mapper.WxGroupQrMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.aiwhose.aiwhosewechatgroup.mapper.WxGroupTypesMapper;
import com.aiwhose.aiwhosewechatgroup.service.WxGroupTypesService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 存储群类型的基本信息，包括每种类型的名称和管理员的二维码 服务层实现。
 *
 * @author 田国刚
 * @since 1.0.1
 */
@Service
public class WxGroupTypesServiceImpl extends ServiceImpl<WxGroupTypesMapper, WxGroupTypes> implements WxGroupTypesService {
   @Resource
   WxGroupTypesMapper wxGroupTypesMapper;
    @Value("${wechat_argument.join_group}")
    private String joinGroup;
    @Autowired
    private WxGroupQrMapper wxGroupQrMapper;

    @Override
    public List<WxGroupTypes> getGroupTypesList(WxGroupTypes wxGroupTypes) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .eq(WxGroupTypes::getPossessor, wxGroupTypes.getPossessor())
                .eq(WxGroupTypes::getId, wxGroupTypes.getId(), StrUtil.isNotBlank(wxGroupTypes.getId()));
        List<WxGroupTypes> list = wxGroupTypesMapper.selectListByQuery(queryWrapper);
        return list;
    }

    @Override
    public Result createGroupTypeAndQrs(WxGroupTypesVO wxGroupTypesVO) {
        WxGroupTypes wxGroupTypes = wxGroupTypesVO.getWxGroupTypes();
        if (StrUtil.isEmpty(wxGroupTypes.getTypeName())){
            wxGroupTypes.setTypeName("群如云 群组");
        }

        List<String> wxGroupQrUrlList = wxGroupTypesVO.getWxGroupQrList();
        String loginId = (String) StpUtil.getLoginId();
        wxGroupTypes.setPossessor(loginId);
        String id = IdUtil.getSnowflakeNextIdStr();
        wxGroupTypes.setId(id);
        wxGroupTypes.setQcValue(joinGroup + wxGroupTypes.getId());
        boolean save = wxGroupTypesMapper.insert(wxGroupTypes)>0;


        List<WxGroupQr> wxGroupQrList = new ArrayList<>();

        for (String url : wxGroupQrUrlList) {
            WxGroupQr wxGroupQr = new WxGroupQr();
            wxGroupQr.setQcName(wxGroupTypes.getTypeName());
            wxGroupQr.setQrCodeImage(url);
            wxGroupQr.setExpirationDate(LocalDateTime.now().plusDays(7));
            wxGroupQr.setTypeId(wxGroupTypes.getId());
            wxGroupQr.setIsActive(true);
            wxGroupQr.setScansCount(3);
            wxGroupQr.setMaxScans(200);
            wxGroupQrList.add(wxGroupQr);
        }
        boolean saveWxGroupQrList = wxGroupQrMapper.insertBatch(wxGroupQrList)>=0;
        if (save && saveWxGroupQrList) {
            return Result.ok("群组新增成功");
        }
        return Result.error("群组新增失败");
    }
}
